package com.cy.builder;

import javafx.scene.input.TouchPoint;
/** 
 * <br>@class: BuilderTest
 * <br>@description: 建造者
 * <br>@author: cy
 * <br>@date: 2020-03-28 21:55:05
 * <br>@version: 1.0
 * <br>
 * <br>@since: (版本) 作者 时间 注释 
 */
public class BuilderTest {
    //需要生成的对象具有复杂的内部结构，且对象内部属性本身相互依赖
    //配合不可变对象使用
    //建造者独立，易于扩展
    //便于控制细节风险,不影响业务处理
    public static void main(String[] args) {
        //如果没有建造者 需要代码顺序保证
        ToyCar tc1 = new ToyCar();
        tc1.setMain_part("华塑主板");
        tc1.setDrive_motor("大成电机");
        tc1.setBattery("二星电池");
        tc1.test();
        //使用建造者
        ToyCar tc2 = new ToyCar.ToyCarBuilder().buildPart1("三系主板").buildPart2("双钻电机").buildPart3("超霸电池").buildPart4("阿萨德轮胎").build();
        tc2.test();
    }
}
class ToyCar{
    static class ToyCarBuilder{
        private String main_part;
        private String drive_motor;
        private String wheel;
        private String battery;

        public ToyCarBuilder buildPart1(String main_part){
            this.main_part = main_part;
            return this;
        }
        public ToyCarBuilder buildPart2(String drive_motor){
            this.drive_motor = drive_motor;
            return this;
        }
        public ToyCarBuilder buildPart3(String battery){
            this.battery = battery;
            return this;
        }
        public ToyCarBuilder buildPart4(String wheel){
            this.wheel = wheel;
            return this;
        }
        ToyCar build(){
            ToyCar tc1 = new ToyCar();
            tc1.setMain_part(this.main_part);
            tc1.setDrive_motor(this.drive_motor);
            tc1.setBattery(this.battery);
            tc1.setWheel(this.wheel);
            return  tc1;
        }
    }

    private String main_part;
    private String drive_motor;
    private String wheel;
    private String battery;

    public String getMain_part() {
        if (main_part == null) {
            throw new RuntimeException("主板缺失");
        }
        return main_part;
    }

    public void setMain_part(String main_part) {
        this.main_part = main_part;
    }

    public String getDrive_motor() {
        if (drive_motor == null) {
            throw new RuntimeException("电机缺失");
        }
        return drive_motor;
    }

    public void setDrive_motor(String drive_motor) {
        if (main_part == null) {
            throw new RuntimeException("主要部件未构建");
        }
        this.drive_motor = drive_motor;
    }

    public String getWheel() {
        if (wheel == null) {
            throw new RuntimeException("轮胎缺失");
        }
        return wheel;
    }

    public void setWheel(String wheel) {
        if (main_part == null) {
            throw new RuntimeException("主要部件未构建");
        }
        this.wheel = wheel;
    }

    public String getBattery() {
        if (battery == null) {
            throw new RuntimeException("电池缺失");
        }
        return battery;
    }

    public void setBattery(String battery) {
        if (main_part == null) {
            throw new RuntimeException("主要部件未构建");
        }
        if (drive_motor == null) {
            throw new RuntimeException("电机部件未构建");
        }
        this.battery = battery;
    }
    public void test(){
        try {
            System.out.println("玩具车出厂测试: 主板="+getMain_part()+"  电机="+getDrive_motor()+" 电池="+getBattery()+" 轮胎="+getWheel());
        }catch (Exception e){
            System.out.println("玩具车出厂测试: 失败");
            e.printStackTrace();
        }
    }
}